<template>
  <div :class="{noInputContent: noContent, jeanInput: true}" :style="{width: width}">
    <input :type="type || 'text'" v-model="jeanInputValue" :placeholder="jeanInputPlaceHolder" class="jeanInputBody" />
    <i class="closeJeanInput iconfont icon-close_circle_surface" v-if="jeanInputValue" @click="clearJeanInputValue"></i>
  </div>
</template>

<script>
export default {
  name: 'jeanInput',
  props: ['maxNum', 'jeanInputPlaceHolder', 'needValidate', 'jeanInputDefaultValue', 'type', 'width'],
  data () {
    return {
      noContent: false,
      jeanInputValue: ''
    }
  },
  mounted () {
    this.jeanInputValue = this.jeanInputDefaultValue || ''
  },
  methods: {
    clearJeanInputValue () {
      this.jeanInputValue = ''
    }
  },
  watch: {
    jeanInputValue (value, oldval) {
      // if (oldval) {
      if (this.needValidate && !value) {
        this.noContent = true
      } else if (value) {
        if (value.toString().indexOf('.') >= 0) {
          this.jeanInputValue = value.toString().split('.')[0]
        }
        if (this.maxNum) {
          if (Number(value) > this.maxNum) {
            this.jeanInputValue = this.maxNum
          }
        }
      }
      this.$emit('setInput', value)
      // }
    },
    jeanInputDefaultValue (val) {
      this.jeanInputValue = val
    }
  }
}
</script>

<style scoped>
  .jeanInput {
    width: 140px;
    height: 25px;
    border: 1px solid #c9c9c9;
    text-align: left;
    line-height: 25px;
    position: absolute;
    padding: 0 5px;
    font-size: 12px;
    top: 2px;
    right: 0;
    border-radius: 3px;
  }
  .jeanInputBody {
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    padding: 0;
    font-size: 13px;
  }
  .closeJeanInput {
    color: #cdcdcd;
    /*color: #ffffff;*/
    font-size: 16px;
    text-align: center;
    line-height: 16px;
    position: absolute;
    right: 3px;
    top: 4.5px;
  }
  .noInputContent {
    border: 1px solid red;
  }
</style>
